import { number } from 'echarts/lib/export';
import Mock from 'mockjs'

var List = [];
for (var i = 0; i < 8; i++) {
    List.push({
        id: Mock.mock('@increment(100120)'),
        classname: '幼儿英语入门课程',
        price: Mock.mock('@integer(200, 500)'),
        sale: Mock.mock('@integer(100, 300)'),
        upload: Mock.mock('@cname'),
        uploadDate: Mock.mock('@date')
    })
}


export default {
    //获取数据
    getCourse: config => {
        console.log(config);
        const { id } = JSON.parse(config.body);
        console.log(id);

        console.log(List)
        let newList = [];

        for (let i = 0; i < List.length; i++) {
            if (List[i].id === id) {
                newList.push(List[i])
            }
        }
        console.log(newList)
        return {
            code: 200,
            msg: '查询成功',
            data: newList != '' ? newList : List,
            total: newList != '' ? newList.length : List.length

        }


    },

    //删除单条数据
    deleteCourse: config => {
        console.log(config)
        const { id } = JSON.parse(config.body);

        for (let i = 0; i < List.length; i++) {
            if (List[i].id === id) {
                List.splice(i, 1);
            }
        }

        return {
            code: 200,
            msg: '删除成功',
            data: List,
        }
    },

    //批量删除数据
    allDelete: config => {
        const { ids } = JSON.parse(config.body);
        console.log(ids)

        const delArr = ids.split(',');
        for (let i = 0; i < List.length; i++) {
            for (let j = 0; j < delArr.length; j++) {
                if (String(List[i].id) === delArr[j]) {
                    List.splice(i, 1);
                }
            }
        }

        return {
            code: 200,
            msg: '批量删除成功',
            data: List
        }
    },

    //编辑数据
    editClass: config => {
        console.log(config);
        const { id, price, sale, upload, uploadDate } = JSON.parse(config.body);

        if (id == 0) {
            List.push({
                id: Mock.mock('@increment(100120)'),
                price: price,
                sale: sale,
                upload: upload,
                uploadDate: uploadDate
            })

        } else {
            for (let i = 0; i < List.length; i++) {
                if (List[i].id === id) {
                    List[i].price = price;
                    List[i].sale = sale;
                    List[i].upload = upload;
                    List[i].uploadDate = uploadDate;
                }
            }
        }



        return {
            code: '200',
            msg: id == 0 ? '新增成功' : '编辑成功',
            data: 'List'
        }







    }

}